<?php
namespace Admin\Controller;
use Think\Controller;
use Myself\FileSystem;
use Admin\Controller\CommonController;
class ShopMangeController extends CommonController {

	protected $statusArr=array("下架","上架");
	protected $isHome=array("未推荐","已推荐");
	
	public   function select_class(){
	
	$list_one_id = I('checkValueOne');
	if( $list_one_id != ' '){
	  $list_two = M('shop_product_class')->where('pid="'.$list_one_id.'"')->select();
	  $list_str.=' <option selected="selected" value="">'.'请选择'.'</option>';
	  foreach($list_two as $key=>$item){
		 $list_str.=' <option  value='.$item['id'].'>'.$item['class_name'].'</option>';
		}
	  } else{
		 $list_str.=' <option selected="selected" value="">'.'请选择'.'</option>';
		}
		
		$this->ajaxReturn( $list_str );
	
	}

public   function select_address(){
	
	$list_one_id = I('checkValueOne');
	if( $list_one_id != ' '){
	  $list_two = M('shop_address')->where('pid="'.$list_one_id.'"')->select();
	  $list_str.=' <option selected="selected" value="">'.'请选择'.'</option>';
	  foreach($list_two as $key=>$item){
		   $list_str.=' <option  value='.$item['id'].'>'.$item['name'].'</option>';
		}
	  }else{
		 $list_str.=' <option selected="selected" value="">'.'请选择'.'</option>';
		}
		
		 $this->ajaxReturn( $list_str );
	
	}

 public function index(){
		 header('Content-Type:text/html;charset=utf-8');
		 //print_r(I('select_name1'));
		 //-----------------搜索-------------------
		
	        $list_one= M('shop_product_class')->where('level=1')->select(); 
	        $list_address= M('shop_address')->where('level=1')->select(); 
		//dump(I('select_name1')); 
	        if(!empty(I('select_name1'))){ 
		   $where['shop_level_one']=I('select_name1');
		}
		if(!empty(I('select_name2'))){
		   $where['shop_level_two']=I('select_name2');
		}
		if(!empty(I('select_name3'))){
		   $where['shop_level_three']=I('select_name3');
		}
		if(!empty(I('select_name4'))){
		   $where['shop_level_four']=I('select_name4');
		}
		if(!empty(I('select_name5'))){
		   $where['shop_prv']=I('select_name5');
		}
		if(!empty(I('select_name6'))){
		   $where['shop_city']=I('select_name6');
		}
		if(!empty(I('select_name7'))){
		   $where['shop_school']=I('select_name7');
		}
		
	
		
		$sh_q = I('sh_q','');//关键字
		if(!empty($sh_q)){
			$shArray['sh_q'] = $sh_q;
		    $map['shop_name'] = array('LIKE','%'.$sh_q.'%');
			$map['id'] = array('LIKE','%'.$sh_q.'%');
			$map['_logic'] = 'OR';
			$where['_complex'] = $map;
		}
		
		

		$from_time = I('from_time','');
		$to_time = I('to_time','');
		  
		if($from_time){
			$from_time_s = strtotime($from_time);
			$where['add_time']= array('EGT',$from_time_s);
			$shArray['from_time'] = $from_time;
		}
		
		if($to_time){
			$to_time_s = strtotime($to_time)+86400;
			$where['add_time']= array('ELT',$to_time_s);
			$shArray['to_time'] = $to_time;	
		}		
		
		if($from_time && $to_time){
			$where['add_time']=array(array('EGT',$from_time_s),array('ELT',$to_time_s),'and');
		}	
                
                $buss_status = I('buss_status','');
                if($buss_status){
                   
			$where['buss_status']= $buss_status;
			$shArray['buss_status'] = $buss_status;
		}
               
                
                $shop_type = I('shop_type','');
                if($shop_type){
                       $where['shop_level_one']= $shop_type;
			$shArray['shop_type'] = $shop_type;
		}
                
                
			  
		if(!empty($shArray)){$this->assign('shArray',$shArray);}// 搜索参数输出
		//-------------------------搜索end-----------------

		$pageno = I('p',1);
                $this->assign('pageno',$pageno);// 赋值数据集
		$pageCount = 10;
                
		 $task = I('task');
                 if(!empty($task) && $task == 100 ){
                      $where['buss_status'] = 1;
                  }
            
	
		$list = M('shop')->where($where)->order('add_time desc, id desc')->page($pageno.','.$pageCount)->select();
		foreach($list as $key=>$item){
		
                        $list[$key]['keeper'] = M('shop_detail')->where('s_id="'.$item['id'].'"')->getField('keeper');
			$list[$key]['class_name'] = M('shop_product_class')->where('id="'.$item['shop_level_one'].'"')->getField('class_name');
			$list[$key]['idcard'] = M('shop_detail')->where('s_id="'.$item['id'].'"')->getField('idcard');
			$list[$key]['bankcard'] = M('shop_detail')->where('s_id="'.$item['id'].'"')->getField('bankcard');
			$list[$key]['bank'] = M('shop_detail')->where('shop_id="'.$item['id'].'"')->getField('bank');
			$list[$key]['build_time'] = M('shop_detail')->where('shop_id="'.$item['id'].'"')->getField('build_time');
			$list[$key]['provin'] = M('shop_address')->where('id="'.$item['shop_prv'].'"')->getField('name');
			$list[$key]['city'] = M('shop_address')->where('id="'.$item['shop_city'].'"')->getField('name');
                        $list[$key]['school'] = M('shop_address')->where('id="'.$item['shop_school'].'"')->getField('name');
			$list[$key]['point'] = M('shop_detail')->where('s_id="'.$item['id'].'"')->getField('point');
			$list[$key]['bank_branch'] = M('shop_detail')->where('shop_id="'.$item['id'].'"')->getField('bank_branch');
		        $list[$key]['phone'] = M('member')->where('id="'.$item['mid'].'"')->getField('phone');
			//$list[$key]['buss_status'] = M('shop_detail')->where('s_id="'.$item['id'].'"')->getField('buss_status');
                        $list[$key]['is_set'] = M('shop_detail')->where('s_id="'.$item['id'].'"')->getField('is_set');
                        $list[$key]['shop_type'] = M('shop_detail')->where('s_id="'.$item['id'].'"')->getField('shop_type');
		}
		
	        $this->assign('list',$list);
		$count = M('shop')->where($where)->count();
		$this->assign('count',$count);
		$Page = new \Think\Page($count,$pageCount);
		$show = $Page->showAdmin();
		$this->assign('page',$show);
                $this->assign('list1',$list_one);
		$buss_status_arr  = array(1=>'待审核',2=>'未展示',3=>'展示中',4=>'禁用'); //营业状态,1待审核 2 未展示  3展示中  4禁用
                $this->assign('buss_status_arr', $buss_status_arr);
                
                $shop_type  = array(1=>'A类商家',2=>'B类影院商家',3=>'B类外卖商家',4=>'C类商家'); //营业状态,1A类商家 2 B类影院商检  3B类外卖商家  4C类商家
                $this->assign('shop_type', $shop_type);
                
                
		$this->assign('listAddress',$list_address);
		$this->assign('statusArr',$this->statusArr);// 状态
		
		$this->display('Shop/index');
    }
	
	
	
	
	  public  function disable(){
	        $buss_status = I('buss_status');
	        $id = I('id');
		if(!empty( $buss_status ) && !empty( $id )){
		
		   if( $buss_status == 1){
		      $data['buss_status'] = 2;
		      $resultA =  M('shop_detail')->where('s_id="'.$id.'"')->save($data);
		   } else{
		      $data['buss_status'] = 1;
		      $resultB =  M('shop_detail')->where('s_id="'.$id.'"')->save($data);
		   }
                   
		   if($resultA || $resultB){
                   $this->redirect(U('shopMange/index'));
                   }
                   
		}
	  }
          
          
            public function busstatus(){
                  $buss_status = I('buss_status');
	          $id = I('id');
                  if( !empty( $buss_status ) && !empty( $id ) ){
                      
                      if($buss_status == 3){
                          $data['buss_status'] = 4;
                          $resultA =  M('shop')->where('s_id="'.$id.'"')->save($data);
                        }
                      
                     if($buss_status == 4 ){
                            $mid = M('shop')->where('id="'.$id.'"')->getField('mid');      
                            $rest_num = M('shop_packet')->where('mid="'.$mid.'"')->getField('rest_num');
                            if($rest_num < 1){
                                 $data['buss_status'] = 2;
                                 $resultB =  M('shop')->where('s_id="'.$id.'"')->save($data);
                              }  else {
                                 $data['buss_status'] = 3;
                                 $resultC =  M('shop')->where('s_id="'.$id.'"')->save($data);
                              }
                      }
                }
               
                
                  if($resultA || $resultB || $resultC){
                   $this->redirect(U('shopMange/index'));
                   }
                   
                   
                
            }

            public  function  is_set(){
                  $is_set = I('is_set');
                  $id = I('id');
                  if(!empty($is_set) && !empty( $id )){
                     
                  if( $is_set == 2){
		      $data['is_set'] = 1;
		      $resultA =  M('shop_detail')->where('s_id="'.$id.'"')->save($data);
		   } else{
		      $data['is_set'] = 2;
		      $resultB =  M('shop_detail')->where('s_id="'.$id.'"')->save($data);
		   }
                   
		   if($resultA || $resultB){
                   $this->redirect(U('shopMange/index'));
                   }
                      
                  }


            }
          
          
	
	public function editPoint(){
		header('Content-Type:text/html;charset=utf-8');
	        $id = I('id',0,'int');
		$dataInfo = M('shop')->find($id);
		$dataInfo['keeper'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('keeper');
                $dataInfo['class_name'] = M('shop_product_class')->where('id="'.$dataInfo['shop_level_one'].'"')->getField('class_name');
		$dataInfo['phone'] = M('member')->where('id="'.$dataInfo['mid'].'"')->getField('phone');
                $dataInfo['telephone'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('telephone');
		$dataInfo['idcard'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('idcard');
                $dataInfo['address'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('address');
		$dataInfo['bankcard'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('bankcard');
		$dataInfo['bank'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('bank');
		$dataInfo['add_time'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('build_time');             
                $dataInfo['provin'] = M('shop_address')->where('id="'.$dataInfo['shop_prv'].'"')->getField('name');
		$dataInfo['city'] = M('shop_address')->where('id="'.$dataInfo['shop_city'].'"')->getField('name');
                $dataInfo['school'] = M('shop_address')->where('id="'.$dataInfo['shop_school'].'"')->getField('name');
		$dataInfo['point'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('point');
		$dataInfo['bank_branch'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('bank_branch');
	//	$dataInfo['buss_status'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('buss_status');
		$dataInfo['liencepic'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('liencepic');
		$this->assign('dataInfo', $dataInfo);
	
		$this->display('Shop/edit');
    }	
	
    
    
	public function editDetail(){
		header('Content-Type:text/html;charset=utf-8');
	        $id = I('id',0,'int');
		$dataInfo = M('shop')->find($id);
		$dataInfo['keeper'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('keeper');
                $dataInfo['class_name'] = M('shop_product_class')->where('id="'.$dataInfo['shop_level_one'].'"')->getField('class_name');
		$dataInfo['phone'] = M('member')->where('id="'.$dataInfo['mid'].'"')->getField('phone');
                $dataInfo['telephone'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('telephone');
		$dataInfo['idcard'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('idcard');
                $dataInfo['address'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('address');
		$dataInfo['bankcard'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('bankcard');
		$dataInfo['bank'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('bank');
		$dataInfo['add_time'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('build_time');             
                $dataInfo['provin'] = M('shop_address')->where('id="'.$dataInfo['shop_prv'].'"')->getField('name');
		$dataInfo['city'] = M('shop_address')->where('id="'.$dataInfo['shop_city'].'"')->getField('name');
                $dataInfo['school'] = M('shop_address')->where('id="'.$dataInfo['shop_school'].'"')->getField('name');
		$dataInfo['point'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('point');
		$dataInfo['bank_branch'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('bank_branch');
		//$dataInfo['buss_status'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('buss_status');
		$dataInfo['liencepic'] = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->getField('liencepic');
		$this->assign('dataInfo', $dataInfo);
		$this->display('Shop/detail');
    }	
	
	
	 public function checkStatus(){
	     $cid = I('cid');
	     $data['buss_status'] = 2;
	     $result = M('shop')->where('id="'.$cid.'"')->save($data);
	     if( $result ){
		 $data = 100;
		 $this->ajaxReturn($data);
		 }
	 
	 }
	 
	 
	   public function savePoint(){
	     $cid = I('cid');
             $data['point'] =  I('kd');
	     $cid = I('cid');
	     $dataInfo = M('shop')->find($cid);
	     $result = M('shop_detail')->where('s_id="'.$dataInfo['id'].'"')->save($data);
		 if( $result ){
		 $data = 100;
		 $this->ajaxReturn($data);
		 }
	 
	   }
}